// pages/banghui/setting.js
const app = getApp()
var util = require('../../utils/util.js')
var http = require('../../utils/server.js')

Page({
  data: {
    jianjie: null,
    data: {}
  },
  onLoad: function(options) {
    var that = this;
    that.data.id = options.gang_id;
    http.post({
      url: "Gang/getInfo",
      gang_id: that.data.id,
      type: ''
    }, function(data) {
      console.log('帮会信息', data);
      that.data.jianjie = data.intro;
      that.setData({
        data: data
      });
    })
  },
  upload: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      success: function(res) {
        var tempFilePaths = res.tempFilePaths

        var token = wx.getStorageSync('token') || "";
        const uploadTask = wx.uploadFile({
          url: 'https://www.ymqvip.com/api/upload/uploadPic',
          filePath: tempFilePaths[0],
          name: 'imgfile',
          formData: {
            'token': token,
            'type': 'center'
          },
          success: function(res) {
            if (res.data.code == 0) {
              var uploadData = JSON.parse(res.data)
              console.log('上传成功', uploadData.data)
              that.data.data.wx_qrcode = uploadData.data;
              that.setData({
                data: that.data.data
              })
            } else {
              util.showModal("上传出错，" + res.message)
              return false;
            }
          },
          fail: function(e) {
            console.log('上传出错', e)
            util.showModal("上传出错，请联系管理员。")
            return false;
          }
        })

        uploadTask.onProgressUpdate((res) => {

        })

      }
    })
  },
  chooseBgPic: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      success: function(res) {
        var tempFilePaths = res.tempFilePaths
        that.setData({
          bgImg: tempFilePaths[0]
        })

        var token = wx.getStorageSync('token') || "";
        const uploadTask = wx.uploadFile({
          url: 'https://www.ymqvip.com/api/upload/uploadPic',
          filePath: tempFilePaths[0],
          name: 'imgfile',
          formData: {
            'token': token,
            'type': 'center'
          },
          success: function(res) {
            var uploadData = JSON.parse(res.data)
            if (uploadData.code == 0) {
              console.log('上传成功', uploadData.data)
              var tdata = that.data.data
              tdata.avatar = uploadData.data;
              that.setData({
                data: tdata
              })
            } else {
              util.showModal("上传出错，" + res.message)
              return false;
            }
          },
          fail: function(e) {
            console.log('上传出错', e)
            util.showModal("上传出错，请联系管理员。")
            return false;
          }
        })

        uploadTask.onProgressUpdate((res) => {

        })

      }
    })
  },

  tojianjie: function() {
    wx.navigateTo({
      url: '/pages/banghui/jianjie',
    })
  },
  formSubmit: function(e) {
    console.info(e);
    var that = this;
    var data = e.detail.value;
    data['intro'] = this.data.jianjie;
    data['url'] = "/gang/editSettings";
    http.post(data, function(res) {

      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2]; //上一个页面（父页面）
      console.info(prevPage);

      prevPage.setData({
        data: that.data.data
      })
      util.showSuccess('设置成功', function() {
        wx.navigateBack({
          delta: 1
        })
      })
    })
  }
})